using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class ProtalGate : Switch
    {
        string _thisId;

        public ProtalCommand ProtalCommand { get; set; }

        

        public ProtalGate(string Id, World world, Stage stage)
            : base(world, stage)
        {
            _thisId = Id;
            _buttonSprite = new Animation("images/system/button", new Rectangle(2, 2, 100, 100), new Point(100, 100));
            CurrentAnimation = new Animation("images/items/protalGate", new Point(60, 100)); 
            Size = new Vector2(60, 100);
        }


        public ProtalGate(string Id, World world, Stage stage, Vector2 v)
            : base(world, stage)
        {
            _thisId = Id;
            _buttonSprite = new Animation("images/system/button", new Rectangle(2, 2, 100, 100), new Point(100, 100));
            CurrentAnimation = new Animation("images/items/protalGate", new Point((int)v.X, (int)v.Y));
            Size = new Vector2(v.X, v.Y);
        }


        public override Vector2 Position
        {
            set
            {
                Vector2 val = value;
                if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.ContainsKey(_thisId))
                {
                    ItemState itemState = new ItemState();
                    itemState.Position = value;
                    try
                    {
                        itemState.StageID = StageManager.CurrentStateID;
                        StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.Add(_thisId, itemState);
                    }
                    catch (ArgumentException)
                    {
                        throw new Exception("An element with Key = " + _thisId + " already exists.");
                    }

                }
                else
                {
                    val = StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[_thisId].Position;
                }

                if (this._body == null)
                    throw new Exception("null body");
                val.X /= PIXEL_TO_METER;
                val.Y = 480 - val.Y;
                val.Y /= PIXEL_TO_METER;
                this._body.SetTransform(val, 0f);

            }
            get
            {
                Vector2 pos = _body.Position * PIXEL_TO_METER;
                pos.Y = 480 - pos.Y;
                return pos;
            }
        }

        public override void Restart()
        {
            //base.Restart();
        }

        public override void ChangeStage(VisibleObject owner, TouchLocationState state)
        {
            //base.ChangeStage(state);
            if (state == TouchLocationState.Pressed)
            {
                // if (ProtalCommand == null)
                //     throw new Exception("null ProtalCommand");
                if (ProtalCommand != null)
                {
                    Transition tra = new DefauldTransition(0, 1, 10, 60);
                    tra.Register(GameStage);
                    tra = new DefauldTransition(1, 1, 58, 70);
                    tra.Register(GameStage);tra.TransitionDoneAction += ChangeStage;
                    tra = new DefauldTransition(1, 1, 58, 80);
                    tra.Register(GameStage);
                    //StageManager.ProtalCommand = ProtalCommand;
                    MusicManager.PlayMusic("protal");
                }

            }
        }

        public void ChangeStage() {
            StageManager.ProtalCommand = ProtalCommand;
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player" && ProtalCommand != null)
            {
                GameStage.ActionButtom.ButtonSprite = null;
                GameStage.ActionButtom.ButtonSprite = _buttonSprite;
            }
            else {
                GameStage.ActionButtom.Enable = false;
                GameStage.ActionButtom.ButtonSprite = null;
            }
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
            //if (contactBody.GetUserData() == "player")
            //{
            //    GameStage.ActionButtom.ButtonSprite = null;
            //}
        }


    }
}
